<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
            v-model="pd.queryParams.name"
            placeholder="请输入名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['fms:classificationItem:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="pd.single"
            @click="handleUpdate"
            v-hasPermi="['fms:classificationItem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="pd.multiple"
            @click="handleDelete"
            v-hasPermi="['fms:classificationItem:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['fms:classificationItem:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" :data="pd.classificationItemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="记账科目编码" align="center" prop="classificationCode"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="租户" align="center" prop="tenantId"/>
      <el-table-column label="创建者" align="center" prop="createBy" width="180"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['fms:classificationItem:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['fms:classificationItem:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="pd.total>0"
        :total="pd.total"
        v-model:page="pd.queryParams.pageNum"
        v-model:limit="pd.queryParams.pageSize"
        @pagination="getList"
    />
    <ClassificationItemCurd ref="classificationItemCurdRef" @ok="classificationItemCurdOk"></ClassificationItemCurd>
  </div>
</template>

<script setup name="Classification">
import {listClassificationItem, getClassificationItem} from "@/api/fms/classificationItem";
import ClassificationItemCurd from "@/views/fms/classification/classificationItemCurd.vue";
import {getCurrentInstance, reactive} from "vue";
const {proxy} = getCurrentInstance();
import {useRoute} from "vue-router";
const route = useRoute();

const pd = reactive({
  classificationItemList: [],
  loading: true,
  showSearch: true,
  ids: [],
  single: true,
  multiple: true,
  total: 0,
  label: {

  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    classificationCode:null,
    name: null,
    tenantId: null,
    createBy: null,
    createTime: null,
  },
});

/** 查询记账项目列表 */
function getList() {
  pd.loading = true;
  listClassificationItem(pd.queryParams).then(response => {
    pd.classificationItemList = response.data;
    pd.total = response.total;
    pd.loading = false;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  pd.queryParams.params = {};
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  pd.ids = selection.map(item => item.id);
  pd.single = selection.length != 1;
  pd.multiple = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  let classificationItem = {};
  proxy.$refs["classificationItemCurdRef"].add(classificationItem);
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || pd.ids
  getClassificationItem(_id).then(res => {
    proxy.$refs["classificationItemCurdRef"].update(res.data);
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || pd.ids;
  console.log(_ids)
  proxy.$refs["classificationItemCurdRef"].del(_ids);
}
/** 对话框完成后刷新界面 */
function classificationItemCurdOk(operation){
  getList();
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('fms/classificationItem/export', {
    ...pd.queryParams
  }, `classificationItem_${new Date().getTime()}.xlsx`)
}
getList();
</script>
